/*************************************************************************
	> File Name: sigtimedwait.c
	> Author: Angel Criss
	> Blog: blog.abbycin.tk 
	> Created Time: Tue 25 Jun 2014 07:52:33 PM CST
 ************************************************************************/
//用sigwaitinfo同步等待信号
#define _GNU_SOURCE
#include<stdio.h>
#include<signal.h>
#include<errno.h>
#include<unistd.h>
#include<string.h>
int main(int argc, char *argv[])
{
    int sig;
    siginfo_t info;
    struct timespec _time;
    sigset_t set;

    _time.tv_sec=10;

    sigfillset(&set);
    
    printf("%s's PID: %d\n",argv[0],getpid());
    printf("\e[44m Now Block Signal\e[0m\n");

    sigprocmask(SIG_SETMASK,&set,NULL); // block all sig except SIGKILL SIGSTOP

    for(;;)
    {
        sig=sigtimedwait(&set,&info,&_time); // Point 如果在time_tv_sec内没收到信号就会调用失败,并自动重新记时
        if(errno)
        {
            printf("ERROR: EAGAIN(wait timeout)\n");
            _exit(errno);
        }
        printf("cought sig: %d(%s)\n",sig,strsignal(sig));
        printf("si_signo %d ,si_code %d ,si_value %d ,si_pid %d\n",
                info.si_signo,info.si_code,info.si_value,info.si_pid);
        if(sig==SIGQUIT)
        {
            printf("The End\n");
            _exit(0);
        }
    }
}
